<?php

/**
 * 
 * @author yuxun
 *
 */
class VersionUpdateCommand extends CConsoleCommand {

	//  */1 * * * * /usr/local/bin/php /data/web/webdata/oss/crontab.php versionUpdate >> /home/webuser/tmp/server_versionUpdate.log
	/**
	 * 服务器定时版本更新(non-PHPdoc)
	 * @see CConsoleCommand::run()
	 */
    public function run($args) {
    	set_time_limit (0);
    	echo "............................... \n";
    	echo "server版本更新定时任务开始 \n";
    	echo "开始时间：".date('Y-m-d H:i:s',time())."\n";
 
        $nowTime = strtotime(date('Y-m-d H:i:00',time()));  //当前时间
        $modVersionUpdate = new ModVersionUpdate();
        //查询设置为当前时间清档的list
        $list = $modVersionUpdate->select('mango', 1, $nowTime);
        
        if(count($list) == 0){
        	echo "暂无符合的数据 \n";
        }

        foreach ($list as $key=>$val){
        	$oss_api = $val['oss_api'];
        	$oss_api = substr($oss_api,7);
        	$oss_api_array = explode(':', $oss_api);
        	$ip = $oss_api_array[0];
        	$develop_id = $val['develop_id'];
        	$update_packet = $val['update_packet'];
        	$id = $val['id'];
        	$server_name = $val['server_name'];
        	$automatic_info = '';
        	
        	echo "服务器：".$server_name." 版本更新"."\n";
        	
        	$exec_l_one = 'sudo /data/sh/web/wupdate_game_version.sh stop '.$develop_id.' '.$ip.' '.$update_packet;
        	echo $exec_l_one."\n";
        	//$exec_l_one = 'whoami';
        	exec($exec_l_one,$output_one,$return_var_one);
        	if($return_var_one == 0 && $output_one[0] == '0'){ //停服成功
        		$automatic_info .= "步骤一：停服成功，脚本输出内容：".$output_one[0]."\n<br/>";
        	}else{
        		$automatic_info .= "步骤一：停服失败，脚本输出内容：".$output_one[0]."\n<br/>";
        		//如果失败，不再继续执行后面的操作
        		echo $automatic_info;
        		$update = array();
        		$update['version_is_automatic'] = 2;
        		$update['version_automatic_info'] = $automatic_info;
        		$modVersionUpdate->updateByPk($id, $update);
        		$message = "服务器：".$server_name." 版本更新情况："."\n";
        		$message .= $automatic_info;
        		self::send($message);
        		continue;
        	}
        	
        	$exec_l_two = 'sudo /data/sh/web/wupdate_game_version.sh check_server '.$develop_id.' '.$ip.' '.$update_packet;
        	echo $exec_l_two."\n";
        	//$exec_l_two = 'whoami';
        	exec($exec_l_two,$output_two,$return_var_two);
        	if($return_var_two == 0 && $output_two[0] == '0'){ 
        		$automatic_info .= "步骤二：检查更新压缩包成功，脚本输出内容：".$output_two[0]."\n<br/>";
        	}else{
        		$automatic_info .= "步骤二：检查更新压缩包失败，脚本输出内容：".$output_two[0]."\n<br/>";
        		//如果失败，不再继续执行后面的操作
        		echo $automatic_info;
        		$update = array();
        		$update['version_is_automatic'] = 2;
        		$update['version_automatic_info'] = $automatic_info;
        		$modVersionUpdate->updateByPk($id, $update);
        		$message = "服务器：".$server_name." 版本更新情况："."\n";
        		$message .= $automatic_info;
        		self::send($message);
        		continue;
        	}
        	
        	$exec_l_three = 'sudo /data/sh/web/wupdate_game_version.sh update_server '.$develop_id.' '.$ip.' '.$update_packet;
        	echo $exec_l_three."\n";
        	//$exec_l_three = 'whoami';
        	exec($exec_l_three,$output_three,$return_var_three);
        	if($return_var_three == 0 && $output_three[0] == '0'){ 
        		$automatic_info .= "步骤三：替换文件成功，脚本输出内容：".$output_three[0]."\n<br/>";
        	}else{
        		$automatic_info .= "步骤三：替换文件失败，脚本输出内容：".$output_three[0]."\n<br/>";
        		//如果失败，不再继续执行后面的操作
        		echo $automatic_info;
        		$update = array();
        		$update['version_is_automatic'] = 2;
        		$update['version_automatic_info'] = $automatic_info;
        		$modVersionUpdate->updateByPk($id, $update);
        		$message = "服务器：".$server_name." 版本更新情况："."\n";
        		$message .= $automatic_info;
        		self::send($message);
        		continue;
        	}
        	
        	$exec_l_four = 'sudo /data/sh/web/wupdate_game_version.sh check_ver '.$develop_id.' '.$ip.' '.$update_packet;
        	echo $exec_l_four."\n";
        	//$exec_l_four = 'whoami';
        	exec($exec_l_four,$output_four,$return_var_four);
        	if($return_var_four == 0 && $output_four[0] == '0'){ 
        		$automatic_info .= "步骤四：检查更新版本号成功，脚本输出内容：".$output_four[0]."\n<br/>";
        	}else{
        		$automatic_info .= "步骤四：检查更新版本号失败，脚本输出内容：".$output_four[0]."\n<br/>";
        		//如果失败，不再继续执行后面的操作
        		echo $automatic_info;
        		$update = array();
        		$update['version_is_automatic'] = 2;
        		$update['version_automatic_info'] = $automatic_info;
        		$modVersionUpdate->updateByPk($id, $update);
        		$message = "服务器：".$server_name." 版本更新情况："."\n";
        		$message .= $automatic_info;
        		self::send($message);
        		continue;
        	}
        	
        	$exec_l_five = 'sudo /data/sh/web/wupdate_game_version.sh start '.$develop_id.' '.$ip.' '.$update_packet;
        	echo $exec_l_five."\n";
        	//$exec_l_five = 'whoami';
        	exec($exec_l_five,$output_five,$return_var_five);
        	if($return_var_five == 0 && $output_five[0] == '0'){ 
        		$automatic_info .= "步骤五：启服成功，脚本输出内容：".$output_five[0]."\n<br/>";
        	}else{
        		$automatic_info .= "步骤五：启服失败，脚本输出内容：".$output_five[0]."\n<br/>";
        		//如果失败，不再继续执行后面的操作
        		echo $automatic_info;
        		$update = array();
        		$update['version_is_automatic'] = 2;
        		$update['version_automatic_info'] = $automatic_info;
        		$modVersionUpdate->updateByPk($id, $update);
        		$message = "服务器：".$server_name." 版本更新情况："."\n";
        		$message .= $automatic_info;
        		self::send($message);
        		continue;
        	}

        	echo $automatic_info;
        	$update = array();
        	$update['version_is_automatic'] = 2;
        	$update['version_automatic_info'] = $automatic_info;
        	$modVersionUpdate->updateByPk($id, $update);
        }

        echo "结束时间：".date('Y-m-d H:i:s',time())."\n";
        echo "server版本更新定时任务结束 \n";
    }
    
    public function request_by_curl($remote_server, $post_string) {
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $remote_server);
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    	curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    	$data = curl_exec($ch);
    	curl_close($ch);
    	 
    	return $data;
    }
    
    /**
     * 通知运维
     * @param unknown $message
     */
    public function send($message){
    	$webhook = "https://oapi.dingtalk.com/robot/send?access_token=8c2794ad7467d95350a661f9f58eb88e225433a89e072c4a21c9e01922479fa4";
    	$data = array ('msgtype' => 'text','text' => array ('content' => $message));
    	$data_string = json_encode($data);
    	$result = self::request_by_curl($webhook, $data_string);
    }

   

        
}